<?php
$loader->requireOnce('ordo/Service.class.php');
$loader->requireOnce('includes/Utils.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');

class C_CompositeService extends Controller {
	
	var $template_mod;					
	
	function C_CompositeService($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'CompositeService') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;				

		$this->view->path = 'composite_service';
	}
	
	public function actionListItemAjax() {
		$compositeId = $_GET['composite_service_id'];
		$ticketId = $_GET['ticket_id'];
		// @Localization
		
		$compositeAsso = Celini::newORDO('CompositeServiceAsso');
		
		$result = $compositeAsso->listCompositeService($compositeId);
		return $this->buildItemHTML($result, $ticketId, $compositeId);
	}
	
	private function buildItemHTML($data, $ticketId, $compositeId) {
		$row = sizeof($data);
		$html = "";
		$html .= "<input id=\"composite_service_id\" type=\"hidden\" value=\"$compositeId\">";
		$html .= "<table id=\"composite_service_item_data\" class=\"details_noalign\" cellspacing=\"0\" cellpaddding=\"0\">";		

		for ($i = 0; $i < $row; $i++) {											
			$html .= $this->addTD($data, $i);			
		}		
		$html .= "</table>";
		return $html;
	}
	
	private function addTD($data, $i) {		
		$serviceId = $data[$i]['service_id'];
		$name = $data[$i]['name'];			
		$disabled = ($data[$i]['optional'] == "no")?" disabled checked ":" ";
		//TODO @Localization		
		$price = $data[$i]['price'];
		$html = "<tr>";
		$html .= "<td><input class=\"composite_service_item_popup\" ".$disabled." id=\"$serviceId\" type=\"checkbox\" value=\"$serviceId\"></td>";
		$html .= "<td align=\"left\" class=\"label\"><label for=\"$serviceId\">$name</label></td>";	
		$html .= "<td align=\"right\">$price</td>";	
		$html .= "</tr>";					
		return $html;
	}
	
	public function actionManagement() {
        return $this->view->render('composite_service_management.html');
    }
    
    public function actionAddCompositeService(){
    	$composite_service_list = Celini::newORDO('CompositeService')->listAllFullInfo();
    	$room_list = Celini::newORDO('Room')->listRooms();
    	$this->assign('room_list', $room_list);
    	$this->assign('CS_list', $composite_service_list);
    	return $this->view->render("composite_service_list.html");
    }
    
// delete composite seervice
	public function actionChangeCompositeServiceStatusAjax() {
    	$compostie_service_id = $_GET['CSId'];
    	$cs = Celini::newORDO('CompositeService', $compostie_service_id);
    	$status = $cs->get('disabled');
        
        if ($status == 0) {
            $newStatus = 1;
        } else if ($status == 1) {
            $newStatus = 0;
        }
        $cs->set('disabled', $newStatus);
        $cs->persist();
    }
    
    public function actionSaveCompositeServiceAjax(){
    	$name = Utils::sanitizeText($_POST['name']);
    	$cs = Celini::newORDO('CompositeService');
    	$duplicate = $cs->checkDuplicateName($name, $_POST['composite_service_id']);
    	if($duplicate == true){
    		return 1;
    	}
		$data = array (
	       	'payment_type' => $_POST['payment_select'],
		   	'room_id' => $_POST['room_select'],
			'disabled' => $_POST['disabled']
	   );
	   if (isset($_POST['composite_service_id']) && $_POST['composite_service_id'] != "") {
            $cs->setup($_POST['composite_service_id']);
       }
       	$id		=	$cs->get_id();
		$l10nData		=	array();
		if($id > 0){
			$field		=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
			$l10nData[$field]	=	$name;
		}
		// Service has no name, add new localized name
		else{
			$supported	=	L10nHelper::getSupportedLocales();
			foreach ($supported as $locale	=>	$country){
				$field		=	L10nHelper::changeField($locale,"name");
				$l10nData[$locale]	=	$name;
			}
		}
	   $cs->populateArray($data);
	   $cs->persist();	 
	}
	
	public function actionChooseRequiredService(){
	 
    	$csList = Celini::newORDO('CompositeService')->listAll();
    	$serviceGroupList = Celini::newORDO('ServiceGroup')->listAll();
		
    	// assign parameters for the view
    	$this->assign('csList', $csList);
    	$this->assign('serviceGroupList', $serviceGroupList);
    	
    	return $this->view->render('view_choose_service.html');
    }
    
	public function actionLoadMappedServiceAjax() {
    	$csId = $_GET['composite_service_id'];
    	// get service list of composite service
    	$mappedList = Celini::newORDO('CompositeServiceAsso')->listCompositeService($csId);
    	return json_encode($this->renderServiceListOfCS($mappedList));
    }
    
	private function renderServiceListOfCS($mappedList) {
    	$body = "";
    	foreach ($mappedList as $row) {
    		$aRow = "";
    		$id = $row['service_id'];
    		$checkedYes = "";
    		$checkedNo = "";
    		if ($row['optional'] == 'yes') {
    			$checkedYes = "checked=\"checked\"";
    		} else {
    			$checkedNo = "checked=\"checked\"";
    		}
    		// buid a row of 'sale_service' table
    		$aRow .= "<tr id=\"$id\">";
    		$aRow .= "<td>
    		              <input type=\"hidden\" id=\"csId[]\" value=\"$id\">
    		              <span>$row[name]</span>
    		          </td>";
    		$aRow .= "<td>
    		              <input type=\"radio\" id=\"required_$id\" name=\"required_$id\" value=\"no\" $checkedNo>".L10nHelper::_("Yes")." 
                          <input type=\"radio\" id=\"required_$id\" name=\"required_$id\" value=\"yes\" $checkedYes>".L10nHelper::_("No")."
                      </td>";
            $aRow .= "<td>
                          <a href=\"javascript:removeService($id);\" title=\"".L10nHelper::_("Delete service")."\"> 
                              <img src=\"../images/remove_small.png\" alt=\"".L10nHelper::_("Delete")."\"></img>
                          </a>
                      </td>";
    		$aRow .= "</tr>";
    		$body .= $aRow;
    	}
    	return array('body' => $body);
    }
    
    public function actionSaveChoseServiceAjax(){
    	$csId = $_POST['composite_service_id'];
    	$csServiceArr = explode(';', $_POST['cs_service_array']);
    	$CompositeServiceAsso = Celini::newORDO('CompositeServiceAsso');
    	// delete old mapping of sale_service
    	$CompositeServiceAsso->deleteServiceOfCS($csId);
    	
    	if(!(isset($csServiceArr[0])) || $csServiceArr[0] == '' ) {
    		return ;
    	}
    	// save new mapping of cs_service
    	foreach ($csServiceArr as $item) {
    		$item = split(",", $item);
    		$newObj = Celini::newORDO('CompositeServiceAsso');
    		$data = array (
                'composite_service_id' => $csId,
                'service_id' => $item[0],
                'optional' => $item[1]
            );
        
            $newObj->populateArray($data);
            $newObj->persist();
    	}
    }	
}
?>